<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加商品 - 积分商城管理</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
        }

        .header {
            background: rgba(255, 255, 255, 0.95);
            padding: 20px;
            border-radius: 15px;
            margin-bottom: 30px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            text-align: center;
        }

        .header h1 {
            color: #667eea;
            font-size: 2.5rem;
            font-weight: 700;
            margin-bottom: 10px;
        }

        .breadcrumb {
            color: #666;
            font-size: 0.9rem;
        }

        .breadcrumb a {
            color: #667eea;
            text-decoration: none;
        }

        .breadcrumb a:hover {
            text-decoration: underline;
        }

        .form-container {
            background: rgba(255, 255, 255, 0.95);
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .form-title {
            color: #667eea;
            font-size: 2rem;
            font-weight: 600;
            margin-bottom: 30px;
            text-align: center;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
            font-size: 0.95rem;
        }

        .form-control {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 1rem;
            transition: all 0.3s ease;
            background: white;
        }

        .form-control:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-control.textarea {
            min-height: 120px;
            resize: vertical;
            font-family: inherit;
        }

        .checkbox-group {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-top: 10px;
        }

        .checkbox {
            width: 18px;
            height: 18px;
            accent-color: #667eea;
        }

        .checkbox-label {
            margin: 0;
            font-weight: 500;
            cursor: pointer;
        }

        .form-help {
            font-size: 0.85rem;
            color: #666;
            margin-top: 5px;
        }

        .form-actions {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 40px;
            flex-wrap: wrap;
        }

        .btn {
            padding: 12px 30px;
            border: none;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            font-size: 1rem;
            min-width: 120px;
            justify-content: center;
        }

        .btn-primary {
            background: #667eea;
            color: white;
        }

        .btn-primary:hover {
            background: #5a6fd8;
            transform: translateY(-2px);
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #545b62;
            transform: translateY(-2px);
        }

        .alert {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-weight: 500;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        @media (max-width: 768px) {
            .container {
                padding: 10px;
            }

            .header h1 {
                font-size: 2rem;
            }

            .form-container {
                padding: 20px;
            }

            .form-actions {
                flex-direction: column;
                align-items: stretch;
            }

            .btn {
                min-width: auto;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header class="header">
            <h1>🛒 积分商城管理</h1>
            <div class="breadcrumb">
                <a href="/admin">管理后台</a> > 
                <a href="/admin/points_shop">积分商城</a> > 
                添加商品
            </div>
        </header>

        <div class="form-container">
            <h2 class="form-title">➕ 添加新商品</h2>

            {% with messages = get_flashed_messages(with_categories=true) %}
                {% if messages %}
                    {% for category, message in messages %}
                        <div class="alert alert-{{ 'success' if category == 'success' else 'error' }}">
                            {{ message }}
                        </div>
                    {% endfor %}
                {% endif %}
            {% endwith %}

            <form method="post">
                <div class="form-group">
                    <label for="name" class="form-label">商品名称 *</label>
                    <input type="text" id="name" name="name" class="form-control" required
                           placeholder="请输入商品名称">
                    <div class="form-help">商品的显示名称，建议简洁明了</div>
                </div>

                <div class="form-group">
                    <label for="description" class="form-label">商品描述</label>
                    <textarea id="description" name="description" class="form-control textarea"
                              placeholder="请输入商品描述，包括商品特点、使用方法等"></textarea>
                    <div class="form-help">详细描述商品的特点和用途</div>
                </div>

                <div class="form-group">
                    <label for="points_cost" class="form-label">所需积分 *</label>
                    <input type="number" id="points_cost" name="points_cost" class="form-control" 
                           min="1" required placeholder="请输入兑换所需积分">
                    <div class="form-help">用户兑换此商品需要消耗的积分数量</div>
                </div>

                <div class="form-group">
                    <label for="stock" class="form-label">库存数量</label>
                    <input type="number" id="stock" name="stock" class="form-control" 
                           min="-1" value="-1" placeholder="请输入库存数量">
                    <div class="form-help">-1 表示无限库存，0 表示缺货，大于 0 表示具体库存数量</div>
                </div>

                <div class="form-group">
                    <div class="checkbox-group">
                        <input type="checkbox" id="is_available" name="is_available" class="checkbox" checked>
                        <label for="is_available" class="checkbox-label">商品可用</label>
                    </div>
                    <div class="form-help">取消勾选将使商品在前台不可见</div>
                </div>

                <div class="form-actions">
                    <button type="submit" class="btn btn-primary">
                        ✅ 添加商品
                    </button>
                    <a href="/admin/points_shop" class="btn btn-secondary">
                        ❌ 取消
                    </a>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 表单验证
        document.querySelector('form').addEventListener('submit', function(e) {
            const name = document.getElementById('name').value.trim();
            const pointsCost = document.getElementById('points_cost').value;
            
            if (!name) {
                alert('请输入商品名称');
                e.preventDefault();
                return;
            }
            
            if (!pointsCost || pointsCost < 1) {
                alert('请输入有效的积分数量');
                e.preventDefault();
                return;
            }
        });

        // 自动保存草稿
        const inputs = document.querySelectorAll('input, textarea');
        inputs.forEach(input => {
            input.addEventListener('input', function() {
                const formData = {
                    name: document.getElementById('name').value,
                    description: document.getElementById('description').value,
                    points_cost: document.getElementById('points_cost').value,
                    stock: document.getElementById('stock').value,
                    is_available: document.getElementById('is_available').checked
                };
                localStorage.setItem('shop_item_draft', JSON.stringify(formData));
            });
        });

        // 恢复草稿
        window.addEventListener('load', function() {
            const draft = localStorage.getItem('shop_item_draft');
            if (draft) {
                try {
                    const formData = JSON.parse(draft);
                    document.getElementById('name').value = formData.name || '';
                    document.getElementById('description').value = formData.description || '';
                    document.getElementById('points_cost').value = formData.points_cost || '';
                    document.getElementById('stock').value = formData.stock || -1;
                    document.getElementById('is_available').checked = formData.is_available !== false;
                } catch (e) {
                    console.log('恢复草稿失败');
                }
            }
        });

        // 提交成功后清除草稿
        document.querySelector('form').addEventListener('submit', function() {
            localStorage.removeItem('shop_item_draft');
        });
    </script>
</body>
</html>